package com.station.client.dragNDrop;

import com.google.gwt.user.client.ui.Grid;
import com.station.client.Base;
import com.station.client.proxies.PCashOffice;
import com.station.client.proxies.PCoordinates;
import com.station.client.proxies.PPetrolPump;
import com.station.client.proxies.PPlaceableEntity;
import com.station.server.entity.Coordinates;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Jimmy87
 * Date: 16.11.13
 * Time: 22:49
 * To change this template use File | Settings | File Templates.
 */
public class CellTable extends Grid {


    final private int CELL_WIDTH = 50;
    final private int CELL_HEIGHT = 50;
    final private int wMAX;
    final private int hMAX;
    private PCoordinates exitCoordinates; //конец шоссе
    private PCoordinates startCoordinates; //начало шоссе
    private PCoordinates inPoint = null; //въезд на азс
    private PCoordinates outPoint = null; //выезд с азс



    private List<PPetrolPump> pumps;

    public DraggableContent currentDraggingItem;
    public Map<String, String> imgMap;
    public boolean isDragMode = true;

    public DropCell[][] dropCells;

    public CellTable(int wMax, int hMax) {

        super(wMax, hMax);
        pumps = new ArrayList<PPetrolPump>();
        setCellPadding(0);
        setCellSpacing(0);

        wMAX = wMax;
        hMAX = hMax;
        imgMap = new HashMap<String, String>(8);
        imgMap.put("pump", "imgs/pump.jpg");
        imgMap.put("exit", "imgs/road_out.jpg");
        imgMap.put("start", "imgs/road_in.jpg");
        imgMap.put("office", "imgs/office.jpg");
        imgMap.put("grass", "imgs/grass.jpg");
        startCoordinates = new PCoordinates(wMax-1,0);
        exitCoordinates = new PCoordinates(wMax-1,hMax-1);


        dropCells = new DropCell[wMax][hMax];

        //i - вертикаль
        //j - горизонталь
        for (int i = 0; i < hMax; i++)
            for (int j = 0; j < wMax; j++) {
                dropCells[i][j] = new DropCell(this, CELL_WIDTH, CELL_HEIGHT, i, j);
                if (j == hMax - 2)
                    dropCells[i][j].setUrl("imgs/fuelAreaBorder.jpg");
                if (i == wMax - 1)
                    dropCells[i][j].setUrl("imgs/road_bottom.jpg");
                setWidget(i, j, dropCells[i][j]);
            }
        //установка заправочного бака
        PCoordinates coordinates = Base.getFuelTankPoint();
        dropCells[coordinates.x][coordinates.y].setUrl("imgs/fuelTankPoint.jpg");


    }

    public void createFieldElements() {
        for (int i = 0; i < wMAX; i++)
            for (int j = 0; j < hMAX; j++) {
                DraggableContent content = dropCells[i][j].getContent();
                if (content != null) {
                    if (content.getEntity() instanceof PPetrolPump) {
                        PPetrolPump pump = (PPetrolPump) content.getEntity();
                        pump.initConnect();
                        pumps.add(pump);
                    }
                    if (content.getEntity() instanceof PCashOffice) {
                        PCashOffice cashOffice = (PCashOffice) content.getEntity();
                        Base.setCashOffice(cashOffice);
                    }

                    if (content.getName().equals("exit"))
                        if (outPoint == null) {
                            outPoint = content.getCoordinates();
                        } else {
                            throw new RuntimeException("More than one exit item on field");
                        }

                    if (content.getName().equals("start")) {
                        if (inPoint == null) {
                            inPoint = new PCoordinates(content.getCoordinates().x,content.getCoordinates().y);
                        } else {
                            throw new RuntimeException("More than one start item on field");
                        }
                    }

                    if (content.getName().equals("office")) {

                    }

                }
            }
    }

    public DraggableContent getCellContent(int i, int j) {
        return dropCells[i][j].getContent();
    }

    public DraggableContent getCurrentItem() {
        return currentDraggingItem;
    }

    public void setCurrentItem(DraggableContent item) {
        currentDraggingItem = item;
    }

    public void setEnable(boolean value) {
        for (int i = 0; i < wMAX; i++)
            for (int j = 0; j < hMAX; j++)
                dropCells[i][j].setEnable(value);
    }

    public List<PPetrolPump> getPumps() {
        return pumps;
    }

    public int getwMAX() {
        return wMAX;
    }

    public int gethMAX() {
        return hMAX;
    }

    public int getCELL_WIDTH() {
        return CELL_WIDTH;
    }

    public int getCELL_HEIGHT() {
        return CELL_HEIGHT;
    }

    public PCoordinates getInPoint() {
        return inPoint;
    }

    public PCoordinates getOutPoint() {
        return outPoint;
    }

    public PCoordinates getExitCoordinates() {
        return exitCoordinates;
    }

    public PCoordinates getStartCoordinates() {
        return startCoordinates;
    }

    public PCoordinates getRefuellerInOutPoint() {
        return new PCoordinates(wMAX - 1,hMAX -2);
    }
}
